<?php

namespace App\Admin\Controllers\Article;

use App\Admin\Repositories\Article\Article;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

use App\Models\Article\Type;

class ArticleController extends AdminController
{
    protected $title = '文章列表';

    protected $description = [
        'index' => '博客文章'
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Article(), function (Grid $grid) {

            //关联查询
            $grid->model()->with('type');


            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('content')->limit(50, '...');
            $grid->column('thumb')->image('', 100, 100);
            // $grid->column('thumb')->image(config('app.url'), 100, 100);
            $grid->column('visit')->width('5%');
            $grid->column('type.name', '所属分类')->width('8%');
            $grid->column('created_at')->date('Y-m-d')->width('15%');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('content');
            $show->field('thumb');
            $show->field('visit');
            $show->field('typeid');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Article(), function (Form $form) {
            // $form->display('id');
            $form->text('title');
            $form->textarea('content');
            $form->image('thumb')->uniqueName();
            $form->text('visit')->readOnly();
            // 下拉框的分类查询
            $form->select('typeid')->options(Type::all()->pluck('name', 'id'));

        
            // $form->display('created_at');
            // $form->display('updated_at');
        });
    }
}
